package org.stvd.controller.common;

import java.io.FileNotFoundException;
import java.io.InputStream;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.FormatType;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.vod.model.v20170321.CreateUploadVideoRequest;
import com.aliyuncs.vod.model.v20170321.CreateUploadVideoResponse;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.aliyuncs.vod.model.v20170321.DeleteVideoResponse;
import com.aliyuncs.vod.model.v20170321.GetPlayInfoRequest;
import com.aliyuncs.vod.model.v20170321.GetPlayInfoResponse;
import com.aliyuncs.vod.model.v20170321.GetVideoInfoRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoInfoResponse;
import com.aliyuncs.vod.model.v20170321.RefreshUploadVideoRequest;
import com.aliyuncs.vod.model.v20170321.RefreshUploadVideoResponse;

/**
 * 
 * @Title: AliyunCommonUtil
 * @Description: 阿里云上传公共工具类
 * @author houzx
 * @date 2020年8月5日
 */
public class AliyunCommonUtil {

    // 访问域名
    private static String endPoint = "http://oss-cn-beijing.aliyuncs.com";
    // 访问秘钥
    private static String accessKeyId = "";
    
    private static String accessKeySecret = "";
    // 存储空间
    private static String bucketName = "";
    // 外网访问地址
    public static String httpPath = "";
    
    public AliyunCommonUtil() {
        super();
    }

    /**
     * 初始化客户端
     * @param accessKeyId
     * @param accessKeySecret
     * @return
     */
    public static DefaultAcsClient initClient() {
        String regionId = "cn-shanghai"; // 点播服务接入区域
        DefaultProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, accessKeySecret);
        DefaultAcsClient client = new DefaultAcsClient(profile);
        return client;
    }
    
    /**
     * @Description: 文件流上传
     * @param content 文件流
     * @param fileName 文件名称
     * @throws FileNotFoundException
     */
    public static void uploadFileAliyun(InputStream content, String fileName) 
        throws FileNotFoundException {
         OSS ossClient = new OSSClientBuilder().build(endPoint, accessKeyId, accessKeySecret);
         if (!ossClient.doesBucketExist(bucketName)) {
            System.out.println("您的Bucket不存在，创建Bucket：" + bucketName + "。");
            ossClient.createBucket(bucketName);
         }
         ossClient.putObject(bucketName, fileName, content);
         ossClient.shutdown();
    }
    
    /**
     * @Description: 获取视频上传地址和凭证
     * @param vodClient
     * @param fileName
     * @param title
     * @return
     * @throws ClientException 
     * @throws ServerException 
     */
    public static CreateUploadVideoResponse createUploadVideo(DefaultAcsClient vodClient, String fileName, String title) 
        throws ServerException, ClientException {
        CreateUploadVideoRequest request = new CreateUploadVideoRequest();
        request.setFileName(fileName);
        request.setTitle(title);
        //request.setDescription("this is desc");
        //request.setTags("tag1,tag2");
        //request.setCoverURL("http://vod.aliyun.com/test_cover_url.jpg");
        //request.setCateId(-1L);
        //request.setTemplateGroupId("");
        //request.setWorkflowId("");
        //request.setStorageLocation("");
        //request.setAppId("app-1000000");
        //设置请求超时时间
        request.setSysReadTimeout(1000);
        request.setSysConnectTimeout(1000);
        return vodClient.getAcsResponse(request);
    }
    
    /**
     * @Description: 刷新视频上传地址和凭证
     * @param vodClient
     * @param videoId
     * @return
     * @throws ClientException
     */
    public static RefreshUploadVideoResponse refreshUploadVideo(DefaultAcsClient vodClient, String videoId) throws ClientException {
        RefreshUploadVideoRequest request = new RefreshUploadVideoRequest();
        request.setAcceptFormat(FormatType.JSON);
        request.setVideoId(videoId);
        //设置请求超时时间
        request.setSysReadTimeout(1000);
        request.setSysConnectTimeout(1000);
        return vodClient.getAcsResponse(request);
    }
    
    /**
     * @Description: 获取上传视频信息
     * @param vodClient
     * @param videoId
     * @return
     * @throws Exception
     */
    public static GetVideoInfoResponse getVideoInfo(DefaultAcsClient vodClient, String videoId) throws Exception {
        GetVideoInfoRequest request = new GetVideoInfoRequest();
        request.setVideoId(videoId);
        return vodClient.getAcsResponse(request);
    }
    
    /**
     * @Description: 获取视频播放信息
     * @param vodClient
     * @param videoId
     * @return
     * @throws Exception
     */
    public static GetPlayInfoResponse getPlayInfo(DefaultAcsClient vodClient, String videoId) throws Exception {
        GetPlayInfoRequest request = new GetPlayInfoRequest();
        request.setVideoId(videoId);
        return vodClient.getAcsResponse(request);
    }
    
    /**
     * @Description: 删除上传视频信息
     * @param vodClient
     * @param videoIds
     * @return
     * @throws Exception
     */
    public static DeleteVideoResponse deleteVideo(DefaultAcsClient vodClient, String videoIds) throws Exception {
        DeleteVideoRequest request = new DeleteVideoRequest();
        // 支持传入多个视频ID，多个用逗号分隔
        request.setVideoIds(videoIds);
        return vodClient.getAcsResponse(request);
    }
}
